package com.globalresources.dao.hibernate;

import java.io.File;
import java.security.acl.Owner;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.globalresources.commons.StringHelper;
import com.globalresources.dao.inface.IFriendDao;
import com.globalresources.po.Friend;
import com.globalresources.po.User;

/**
 * @author aaron.lu(luzhiyuan) E-mail:atyuan.lu@gmail.com
 * @since 创建时间：Aug 6, 2009 11:37:07 AM
 */
public class FriendDaoHibernate extends HibernateDaoSupport implements IFriendDao{

	/**
	 * <p>查询好友列表
	*@author aaron.lu(luzhiyuan)
	*@param nickName
	*@param onwerName
	*@param isInBacklist
	*@param pageSize
	*@param startRow
	*@return
	 */
	public List<Friend> getFriends(String nickName, String onwerName, Byte isInBacklist, final int pageSize, final int startRow){
		final StringBuffer hql = new StringBuffer("select f from Friend f left outer join f.userByOwnerId where 1=1 ");
		if (StringHelper.isNotEmpty(nickName)) {
			hql.append(" and f.nickName='"+nickName+"'");
		}
		if (StringHelper.isNotEmpty(onwerName)) {
			hql.append(" and f.userByOwnerId.username = '"+onwerName+"'");
		}
		if (null != isInBacklist) {
			hql.append(" and f.isInBacklist = "+isInBacklist+"");
		}
		return (List<Friend>)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql.toString());
				query.setFirstResult(startRow);
				query.setMaxResults(startRow + pageSize);
				return query.list();
			}
		});
	}
	
	/**
	 * <P>统计好友数目
	*@author aaron.lu(luzhiyuan)
	*@param onwerName
	*@param isInBacklist
	*@return
	 */
	public Long countFriends(final String  onwerName, final Byte isInBacklist){
		return (Long)getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session){
			    Query query = session.createQuery("select count(*) from Friend f where f.userByOwnerId.username = ? and f.isInBacklist=?");
			    query.setParameter(0, onwerName);
			    query.setParameter(1, isInBacklist);
			    Long count = (Long)query.uniqueResult();
				return count;
			}
		});
	}
	
	/**
	 * <p>保存或更新好友信息
	*@author aaron.lu(luzhiyuan)
	*@param friend
	 */
	public void saveOrUpdateFriend(Friend friend){
		getHibernateTemplate().saveOrUpdate(friend);
	}
	
	/**
	 * <p>删除好友信息
	*@author aaron.lu(luzhiyuan)
	*@param friend
	 */
	public void deleteFriend(Friend friend){
		getHibernateTemplate().delete(friend);
	}
	
	/**
	 * <p>获取好友信息
	*@author aaron.lu(luzhiyuan)
	*@param id
	*@return
	 */
	public Friend loadFriend(Integer id){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Friend.class).add(Property.forName("id").eq(id));
		return (Friend)getHibernateTemplate().findByCriteria(detachedCriteria).get(0);
	}
}
